<?php
namespace DataAPI\Controller;


use DataAPI\Model\BproductsModel;
use DataAPI\Model\SupplierProductsModel;
use Think\Controller;

header('Content-Type:text/html; charset=utf-8');

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/2 0002
 * Time: 上午 11:22
 */
class BproductsController extends Controller {

    public function test(){
        echo '开始';
    }
    public function index(){
        set_time_limit(0);
        $OdooApi = new OdooApiController();
        $OdooApi->_initializeB();
        $productsIds = $OdooApi->getallskockBids();

//        $productsIds = $OdooApi->getallskockB();print_r($productsIds);die;
        $num = 5;
        $ids = array_chunk($productsIds, $num);

        $count = ceil(count($productsIds)/5);
        for($i=0;$i<+$count;$i++){
            $products = $OdooApi->getProductsInfo($ids[$i]);

            $insertDatas=array();
            foreach($products as $productId=>$product){
                $insertDatas[$productId]['earn13'] = $product['ean13'];         //商品en13码
                $insertDatas[$productId]['name'] = $product['name'];            //商品名称
                $insertDatas[$productId]['info_keywords'] = '';                 //商品关键字
                $insertDatas[$productId]['weight'] = $product['weight'];        //产品重量
                $insertDatas[$productId]['pom'] = $product['uom_po_id'][1];            //计量单位
                $insertDatas[$productId]['info_pc'] = '';        //电脑端商品详情
                $insertDatas[$productId]['info_mobile'] = '';    //手机端商品详情
                $insertDatas[$productId]['market_price'] = $product['list_price'] * 1.3;   //市场价
                $insertDatas[$productId]['acting_price'] = $product['list_price'];   //一件代发价格
                $insertDatas[$productId]['wholesale_price'] = $product['list_price'] * 1.1;//批发价
                $insertDatas[$productId]['lock_price'] = $product['list_price'] * 1.2;     //锁货价
                $insertDatas[$productId]['wholesale_start_num'] = '100';//最低批发数量
                $insertDatas[$productId]['lock_start_num'] = '50'; //最低锁货数量
                $insertDatas[$productId]['price'] = $product['list_price'] * 1.5;          //建议零售价
                $insertDatas[$productId]['stock_num'] = $product['virtual_available'];      //库存
                $insertDatas[$productId]['warehouse_id'] = $product['product_variant_ids'][0];   //仓库ID
                $insertDatas[$productId]['b_id'] = $product['id'];   //B库商品id
            }
            $this->addProducts($insertDatas);
        }

    }

    /**
     * 添加商品
     * @param null $insertDatas
     */
    public function addProducts($insertDatas = null){
        $n = 1;
        $productModel = new SupplierProductsModel(10007);
//        $OdooApi = new OdooApiController();
//        $OdooApi->_initializeB();
//        $productsIds = $OdooApi->getallskockB();
//        foreach($productsIds as $productId=>$product){
//            $insertDatas[$productId]['earn13'] = $product['ean13'];         //商品en13码
//            $insertDatas[$productId]['name'] = $product['name'];            //商品名称
//            $insertDatas[$productId]['info_keywords'] = '';                 //商品关键字
//            $insertDatas[$productId]['weight'] = $product['weight'];        //产品重量
//            $insertDatas[$productId]['pom'] = $product['uom_id'][1];            //计量单位
//            $insertDatas[$productId]['info_pc'] = '';        //电脑端商品详情
//            $insertDatas[$productId]['info_mobile'] = '';    //手机端商品详情
//            $insertDatas[$productId]['market_price'] = $product['list_price'] * 1.3;   //市场价
//            $insertDatas[$productId]['acting_price'] = $product['list_price'];   //一件代发价格
//            $insertDatas[$productId]['wholesale_price'] = $product['list_price'] * 1.1;//批发价
//            $insertDatas[$productId]['lock_price'] = $product['list_price'] * 1.2;     //锁货价
//            $insertDatas[$productId]['wholesale_start_num'] = '100';//最低批发数量
//            $insertDatas[$productId]['lock_start_num'] = '50'; //最低锁货数量
//            $insertDatas[$productId]['price'] = $product['list_price'] * 1.5;          //建议零售价
//            $insertDatas[$productId]['stock_num'] = $product['virtual_available'];      //库存
//            $insertDatas[$productId]['warehouse_id'] = $product['product_variant_ids'][0];   //仓库ID
//        }

        foreach ($insertDatas as $insertData){
            $ex = $productModel->addProducts($insertData);

            if(!$ex){
                $message = $n++.'条失败或者已经存在';
                continue;
            }else{
                $message = '本次成功导入'.$n++.'条数据';
            }

        }
        echo $message;
    }

    /**
     * 测试，获取商品接口
     * @param null $rarn13
     */
    public function getOne($rarn13=null){
        $ids = array(1000041545,1000041543,1000041546);
        $OdooApi = new OdooApiController();
        $OdooApi->_initializeB();
//        $productsIds = $OdooApi->getallskockB();
        $products = $OdooApi->getProductsInfo($ids);
        print_r($products);die;
    }
}